package com.ktjy.car.mapper;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.car.entity.Complaint;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ComplaintMapper extends BaseMapper<Complaint> {
    @Select("SELECT c.id, c.problem, c.brandId, c.status, " +
            "c.carType, c.complaintsTime, b.brandName " +
            "FROM vehiclecomplaints c LEFT JOIN brand b ON c.brandId = b.brandId " +
            "ORDER BY c.complaintsTime DESC")
    IPage<Complaint> findPage(Page<Complaint> page);

    @Select("<script>" +
            "SELECT c.id, c.problem, c.brandId, c.status, " +
            "c.carType, c.complaintsTime, b.brandName " +
            "FROM vehiclecomplaints c " +
            "LEFT JOIN brand b ON c.brandId = b.brandId " +
            "<if test='ew.customSqlSegment != null'>" +
                "${ew.customSqlSegment}" +
            "</if>" +
            "</script>")
    IPage<Complaint> searchPage(Page<Complaint> page, @Param("ew") QueryWrapper<Complaint> queryWrapper);

    @Insert("INSERT INTO vehiclecomplaints(problem, brandId, status, carType, complaintsTime) " +
            "VALUES(#{problem}, #{brandId}, #{status}, #{carType}, #{complaintsTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Complaint complaint);

    @Update("UPDATE vehiclecomplaints SET status = #{status} WHERE id = #{id}")
    int update(@Param("id") Long id, @Param("status") String status);
}